Page({
  data: {
    block: '',
    goods_list:[],
    sort:'',
    sort_price:{"sort_price":1},
    sort_discount:{"sort_discount":1},
    sort_sell_count:{}
  },

  detailTap(e){
    var id = e.currentTarget.id;
    // console.log(e);
    my.navigateTo({
      url: `/pages/main/goods-detail/goods-detail?id=${id}`, // 需要跳转的 tabBar 页面的路径（需在 app.json 的 tabBar 字段定义的页面），路径后不能带参数
    });
  },

  sortSellCountTap(){
         var app = getApp()
       var sort = {};
       console.log(this.data.sort_sell_count.sort_sell_count);
      if(this.data.sort_sell_count.sort_sell_count == 1)
    {
       sort = {"sort_sell_count":2,"access_token":app.globadData.access_token};
      //  this.data.sort.sort = 2;
    }
    else{
       sort = {"sort_sell_count":1,"access_token":app.globadData.access_token};
      //  this.data.sort.sort = 1;
       
    }
    
       this.setData({
          sort_sell_count:sort,
          sort : sort
        });
        
        this.sort();
  },

  sortPriceTap(){
    var app = getApp()
    var sort = {};
    console.log(this.data.sort_price.sort_price);
    if(this.data.sort_price.sort_price == 1)
    {
       sort = {"sort_price":2,"access_token":app.globadData.access_token};
      //  this.data.sort.sort = 2;
    }
    else{
       sort = {"sort_price":1,"access_token":app.globadData.access_token};
      //  this.data.sort.sort = 1;
       
    }
    
       this.setData({
          sort_price:sort,
          sort : sort
        });
        
        this.sort();
      },
      
      sortDiscountTap(){
        var app = getApp()
    var sort = {};
    console.log(this.data.sort_discount.sort_discount);
    if(this.data.sort_discount.sort_discount == 1)
    {
       sort = {"sort_discount":2,"access_token":app.globadData.access_token};
      //  this.data.sort.sort = 2;
    }
    else{
       sort = {"sort_discount":1,"access_token":app.globadData.access_token};
      //  this.data.sort.sort = 1;
       
    }
    
       this.setData({
          sort_discount:sort,
          sort:sort
        });
        
        this.sort();
      },

  //排序
  sort(){
            var app = getApp()
      var url = app.globadData.apiUrlHeader+'get-goods-list'
      // var key = this.data.sort.sort_type;
      // var value = this.data.sort.sort;

      // var query_word = {key:value,"access_token":app.globadData.access_token};
     
     
      console.log(this.data.sort);
      my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify(this.data.sort),
        dataType:'json',
        success: (res) => {
          console.log(res);
          
        if(res.data.status==0)
        {
          console.log(res);
           this.setData({
              goods_list : res.data.data
           })
          // console.log(img);
          
        }
      },
      fail:(res)=>{
          console.log(res);
        
      },
    });
    },

  onLoad(query) { 
    console.log(query.search_word);
    console.log(query);
    var app = getApp()
    var url = app.globadData.apiUrlHeader+'get-goods-list';
     var query_word = [];
     console.log(query.search_word);
     console.log(query.category_id);
    // if(query.search_word != '' && query.search_word != undefined){
      //返回查询列表
      console.log('in if');
      var val = query.search_word;

     

      if(query.search_word != undefined){
        query_word = {"key_word":query.search_word,"access_token":app.globadData.access_token};
      }else if(query.query_name != undefined){
        var tmp = query.query_name;
        //key 不能传递参数
        if(tmp == 'is_free_express')
         query_word = {"is_free_express":"1","access_token":app.globadData.access_token};
         else if(tmp=='is_recommend')
           query_word = {"is_recommend":"1","access_token":app.globadData.access_token};
           else if(tmp=="is_promotion")
             query_word = {"is_promotion":"1","access_token":app.globadData.access_token};
             else if(tmp=="is_newest")
             query_word = {"is_newest":"1","access_token":app.globadData.access_token};
             else if(tmp=="is_hot")
             query_word = {"is_hot":"1","access_token":app.globadData.access_token};
        }
    else if(query.category_id != '' && query.category_id != undefined){
      console.log('in else if');
      
      query_word = {"category_id":query.category_id,
      "access_token":app.globadData.access_token};
    }else{
      query_word = {"key_word":query.search_word,
                    "access_token":app.globadData.access_token};
      
    }
        console.log(query_word);
      
      my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify(query_word),
        dataType:'json',
        success: (res) => {
          console.log(res);
          
        if(res.data.status==0)
        {
          console.log(res);
           this.setData({
              goods_list : res.data.data
           })
          // console.log(img);
          
        }
      },
      fail:(res)=>{
          console.log(res);
        
      },
    });
    
  },
  search(e) {
    console.log(e);
    var id = e.target.id
    console.log(id);

  },

  switchShow() {
    if(this.data.block == ''){
      this.setData(
        {
          block:'block'
        }
      );
    }else{
      this.setData(
        {
          block:''
        }
      );
    }
  },

  toDetail(e) {
    var id = e.currentTarget.id;
    console.log(e);
    my.navigateTo({
      url: `/pages/main/goodsDetail/goodsDetail?id=${id}`, // 需要跳转的 tabBar 页面的路径（需在 app.json 的 tabBar 字段定义的页面），路径后不能带参数
    });
  },
});
